package com.tony.business.util;

import com.alibaba.fastjson.JSONObject;
import com.tony.domain.bo.UserInfo;
import com.tony.util.utils.JwtUtil;
import io.jsonwebtoken.Claims;

import java.util.concurrent.TimeUnit;

/**
 * @author x
 * @date 2020/5/29 16:41
 */
public class TokenUtil {

    private TokenUtil() {
    }

    public static String createToken(UserInfo jwtbo) {
        return JwtUtil.createJWT(jwtbo.getUserId().toString(), JSONObject.toJSONString(jwtbo), TimeUnit.MINUTES.toMillis(30));
    }

    public static UserInfo getJWTBo(String token) {
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        return JSONObject.parseObject(subject, UserInfo.class);
    }

    public static String refreshToken(String token) {
        return JwtUtil.refreshToken(token, TimeUnit.MINUTES.toMillis(30));
    }
}
